﻿using System.Linq;
using NHibernate;
using NUnit.Framework;
using OpenWaves.ActivityFeed.NHibernate;
using OpenWaves.ActivityFeed.UnitTests;

namespace OpenWaves.ActivityFeed.UnitTests_NHibernate
{
    [TestFixture]
    public class NHibernateActivityFeedServiceTests : ActivityFeedServiceTests
    {
        private ISessionFactory _sessionFactory;

        [SetUp]
        public void InitializeSessionFactory()
        {
            this._sessionFactory = NHibernateTestsUtils.CreateNHibernateTestsSessionFactory().CreateSessionFactory();
        }

        protected override IActivityFeedTestSession CreateSession()
        {
            var session = _sessionFactory.OpenSession();
            session.FlushMode = FlushMode.Commit;
            var transaction = session.BeginTransaction();

            return new NHibernateActivityFeedTestSession(session, transaction);
        }

        [Test]
        public void When_users_subscribe_to_topics_Then_GetSubscribers_returns_distinct_subscriptions_for_specified_subscribers()
        {
            const string firstPublisher = "1";
            const string anotherPublisher = "2";

            When(service =>
            {
                service.Subscribe(firstPublisher, new[] { new Topic("topic1") });
                service.Subscribe(anotherPublisher, new[] { new Topic("topic1") });
                service.Subscribe(anotherPublisher, new[] { new Topic("topic2") });
            });

            Then(service =>
            {
                var subscribers = service.GetSubscribers().WithIds(new string[] { "1", "2" });

                var topics = subscribers.SelectMany(s => s.Subscriptions).Distinct().ToList();

                Assert.That(topics.Count(), Is.EqualTo(2));
            });
        }
    }
}